package com.imooc.common.utils.common;

import java.math.BigDecimal;

/**
 * BigDecimal的加减乘除
 */
public class BigDecimalUtil {

    /**
     * 默认保留小数
     */
    public static final int DEFAULT_SCALE = 2;

    /**
     * 默认四舍五入
     */
    public static final int DEFAULT_ROUNDING_MODE = BigDecimal.ROUND_HALF_UP;

    /**
     * BigDecimal操作枚举定义
     */
    public enum BigDecimalOprations{
        ADD,SUBTRACT,MULTIPLY,DIVIDE
    }

    /**
     * Bigdecimal加减乘除运算
     * @param numOne [String Integer Long Double Bigdecimal]
     * @param numTwo [String Integer Long Double Bigdecimal]
     * @param bigDecimalOpration
     * @param scale
     * @param roundingMode
     * @return
     * @throws Exception
     */
    public static BigDecimal operationASMD(Object numOne,Object numTwo,BigDecimalOprations bigDecimalOpration,int scale,int roundingMode){
        BigDecimal num1 = numOne==null?BigDecimal.ZERO:new BigDecimal(String.valueOf(numOne)).setScale(scale,roundingMode);
        BigDecimal num2 = numTwo==null?BigDecimal.ZERO:new BigDecimal(String.valueOf(numTwo)).setScale(scale,roundingMode);
        switch (bigDecimalOpration){
            case ADD: return num1.add(num2).setScale(scale,roundingMode);
            case SUBTRACT: return num1.subtract(num2).setScale(scale,roundingMode);
            case MULTIPLY: return num1.multiply(num2).setScale(scale,roundingMode);
            case DIVIDE: return num2.intValue()==0?BigDecimal.ZERO:num1.divide(num2, scale, roundingMode);
            default: throw new RuntimeException("操作符有误!");
        }
    }

    /**
     * Bigdecimal加减乘除运算(默认四舍五入 保留两位小数)
     * @param numOne [String Integer Long Double Bigdecimal]
     * @param numTwo [String Integer Long Double Bigdecimal]
     * @param bigDecimalOpration 操作符
     * @param scale 保留几位小数
     * @return bigDecimal
     */
    public static BigDecimal operationASMD(Object numOne,Object numTwo,BigDecimalOprations bigDecimalOpration,int scale){
        BigDecimal num1 = numOne==null?BigDecimal.ZERO:new BigDecimal(String.valueOf(numOne)).setScale(scale,DEFAULT_ROUNDING_MODE);
        BigDecimal num2 = numTwo==null?BigDecimal.ZERO:new BigDecimal(String.valueOf(numTwo)).setScale(scale,DEFAULT_ROUNDING_MODE);
        switch (bigDecimalOpration){
            case ADD: return num1.add(num2).setScale(scale,DEFAULT_ROUNDING_MODE);
            case SUBTRACT: return num1.subtract(num2).setScale(scale,DEFAULT_ROUNDING_MODE);
            case MULTIPLY: return num1.multiply(num2).setScale(scale,DEFAULT_ROUNDING_MODE);
            case DIVIDE: return num2.intValue()==0?BigDecimal.ZERO:num1.divide(num2, scale, DEFAULT_ROUNDING_MODE);
            default: throw new RuntimeException("操作符有误!");
        }
    }

    /**
     * Bigdecimal加减乘除运算(默认四舍五入 保留两位小数)
     * @param numOne [String Integer Long Double Bigdecimal]
     * @param numTwo [String Integer Long Double Bigdecimal]
     * @param bigDecimalOpration
     * @return
     * @throws Exception
     */
    public static BigDecimal operationASMD(Object numOne,Object numTwo,BigDecimalOprations bigDecimalOpration){
        BigDecimal num1 = numOne==null?BigDecimal.ZERO:new BigDecimal(String.valueOf(numOne)).setScale(DEFAULT_SCALE,DEFAULT_ROUNDING_MODE);
        BigDecimal num2 = numTwo==null?BigDecimal.ZERO:new BigDecimal(String.valueOf(numTwo)).setScale(DEFAULT_SCALE,DEFAULT_ROUNDING_MODE);
        switch (bigDecimalOpration){
            case ADD: return num1.add(num2).setScale(DEFAULT_SCALE,DEFAULT_ROUNDING_MODE);
            case SUBTRACT: return num1.subtract(num2).setScale(DEFAULT_SCALE,DEFAULT_ROUNDING_MODE);
            case MULTIPLY: return num1.multiply(num2).setScale(DEFAULT_SCALE,DEFAULT_ROUNDING_MODE);
            case DIVIDE: return num2.intValue()==0?BigDecimal.ZERO:num1.divide(num2, DEFAULT_SCALE, DEFAULT_ROUNDING_MODE);
            default: throw new RuntimeException("操作符有误!");
        }
    }

    public static void main(String[] args) throws Exception {
        BigDecimal bigDecimal = BigDecimalUtil.operationASMD(20, null, BigDecimalOprations.DIVIDE, 4);
        System.out.println(bigDecimal.toString());
    }
}
